#!/bin/bash

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印函数
print_info() {
    echo -e "${BLUE}ℹ${NC} $1"
}

print_success() {
    echo -e "${GREEN}✓${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

# 检查是否在 git 仓库中
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    print_error "不在 git 仓库中"
    exit 1
fi

# 检查工作区是否干净
if [[ -n $(git status -s) ]]; then
    print_warning "工作区不干净，存在未提交的更改："
    git status -s
    read -p "是否继续? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# 获取当前版本
if [ -f "VERSION" ]; then
    CURRENT_VERSION=$(cat VERSION)
    print_info "当前版本: v$CURRENT_VERSION"
else
    CURRENT_VERSION="0.0.0"
    print_warning "VERSION 文件不存在，假设当前版本为 v$CURRENT_VERSION"
fi

# 询问新版本号
echo
print_info "请输入新版本号 (格式: X.Y.Z):"
read -p "新版本: " NEW_VERSION

# 验证版本号格式
if ! [[ $NEW_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    print_error "版本号格式无效。应为: X.Y.Z (例如: 1.0.0)"
    exit 1
fi

# 确认发布
echo
print_warning "准备发布版本: v$NEW_VERSION"
read -p "确认发布? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    print_info "取消发布"
    exit 0
fi

print_info "开始发布流程..."
echo

# 1. 更新 VERSION 文件
print_info "更新 VERSION 文件..."
echo "$NEW_VERSION" > VERSION
print_success "VERSION 文件已更新"

# 2. 更新 Makefile
print_info "更新 Makefile..."
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    sed -i '' "s/^VERSION=.*/VERSION=$NEW_VERSION/" Makefile
else
    # Linux
    sed -i "s/^VERSION=.*/VERSION=$NEW_VERSION/" Makefile
fi
print_success "Makefile 已更新"

# 3. 检查 CHANGELOG.md
print_info "检查 CHANGELOG.md..."
if ! grep -q "\[$NEW_VERSION\]" CHANGELOG.md; then
    print_warning "CHANGELOG.md 中未找到 v$NEW_VERSION 的更新日志"
    print_info "请手动添加更新日志，然后按回车继续..."
    read
fi
print_success "CHANGELOG.md 检查完成"

# 4. 运行测试
print_info "运行测试..."
if go test ./... > /dev/null 2>&1; then
    print_success "所有测试通过"
else
    print_warning "测试失败，但继续发布流程"
fi

# 5. 代码检查
print_info "运行代码检查..."
if go vet ./... > /dev/null 2>&1; then
    print_success "代码检查通过"
else
    print_warning "代码检查发现问题，但继续发布流程"
fi

# 6. 格式化代码
print_info "格式化代码..."
go fmt ./... > /dev/null 2>&1
print_success "代码格式化完成"

# 7. 提交更改
print_info "提交更改..."
git add VERSION Makefile CHANGELOG.md
git commit -m "chore: bump version to v$NEW_VERSION"
print_success "更改已提交"

# 8. 创建标签
print_info "创建 Git 标签..."
git tag -a "v$NEW_VERSION" -m "Release version $NEW_VERSION"
print_success "标签 v$NEW_VERSION 已创建"

# 9. 推送到远程
print_info "推送到远程仓库..."
git push origin main
git push origin "v$NEW_VERSION"
print_success "已推送到远程仓库"

# 10. 完成
echo
print_success "=========================================="
print_success "  发布 v$NEW_VERSION 完成！"
print_success "=========================================="
echo
print_info "GitHub Actions 将自动构建并创建 Release"
print_info "查看发布进度: https://github.com/yourusername/kscan/actions"
print_info "查看发布页面: https://github.com/yourusername/kscan/releases/tag/v$NEW_VERSION"
echo